/**
 * 
 */
package pt.mleiria.web.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.PropertyConfigurator;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import pt.mleiria.bo.TextLoaderBO;

/**
 * @author root
 *
 */
public class IntroController implements Controller {
	private Properties properties = new Properties();
	private TextLoaderBO textLoaderBO;
	/**
	 * @return the textLoaderBO
	 */
	public TextLoaderBO getTextLoaderBO() {
		return textLoaderBO;
	}

	/**
	 * @param textLoaderBO the textLoaderBO to set
	 */
	public void setTextLoaderBO(TextLoaderBO textLoaderBO) {
		this.textLoaderBO = textLoaderBO;
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.Controller#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		final String lang = request.getParameter("lang");
		try {
			properties.load(getClass().getResourceAsStream("/application.properties"));
			if(lang != null && lang.equals("pt_PT")){
				properties.load((getClass().getResourceAsStream("/messages_pt_PT.properties")));
				request.getSession().setAttribute("lang", "pt_PT");
				request.getSession().setAttribute("intro", textLoaderBO.loadIntro("pt_PT").toString());
			}else{
				properties.load((getClass().getResourceAsStream("/messages_en_UK.properties")));
				request.getSession().setAttribute("lang", "en_UK");
				request.getSession().setAttribute("intro", textLoaderBO.loadIntro("en_UK").toString());
			}
		}catch (IOException e) {
			e.printStackTrace();
		}
		PropertyConfigurator.configure(properties);
		loadMenu(request);
		return new ModelAndView("intro");
	}
	private void loadMenu(HttpServletRequest request){
		//RequestContext rc = new RequestContext(request);
		Map<String, String> menuMap = new HashMap<String, String>();
		menuMap.put("regiao", properties.getProperty("message.regiao"));
		menuMap.put("geografia", properties.getProperty("message.geografia"));
		menuMap.put("geografiaLink", "geography.htm");
		menuMap.put("historia", properties.getProperty("message.historia"));
		menuMap.put("historiaLink", "history.htm");
		menuMap.put("gastronomia", properties.getProperty("message.gastronomia"));
		menuMap.put("gastronomiaLink", "gastronomy.htm");
		menuMap.put("casa", properties.getProperty("message.casa"));
		menuMap.put("jardim", properties.getProperty("message.jardim"));
		menuMap.put("jardimLink", "garden1.htm");
		menuMap.put("quartos", properties.getProperty("message.quartos"));
		menuMap.put("quartosLink", "rooms.htm");
		menuMap.put("sala", properties.getProperty("message.sala"));
		menuMap.put("salaLink", "livingrooms.htm");
		menuMap.put("slideshow", properties.getProperty("message.slideshow"));
		menuMap.put("slideshowLink", "slideshow1.htm");
		menuMap.put("contactos", properties.getProperty("message.contactos"));
		menuMap.put("contactosLink", "contacts.htm");
		menuMap.put("precos", properties.getProperty("message.precos"));
		menuMap.put("precosLink", "prices.htm");
		menuMap.put("livrovisitas", properties.getProperty("message.livrovisitas"));
		menuMap.put("livrovisitasLink", "guestbook.htm");
		menuMap.put("todo", properties.getProperty("message.todo"));
		menuMap.put("todoLink", "todo.htm");
		menuMap.put("anterior", properties.getProperty("message.anterior"));
		menuMap.put("seguinte", properties.getProperty("message.seguinte"));
		menuMap.put("obrigado", properties.getProperty("message.obrigado"));
		menuMap.put("nome", properties.getProperty("message.nome"));
		menuMap.put("comentario", properties.getProperty("message.comentario"));
		//Contacts
		Map<String, Object> contactsMap = new HashMap<String, Object>();
		contactsMap.put("morada", properties.getProperty("message.morada"));
		contactsMap.put("telefone", properties.getProperty("message.telefone"));
		contactsMap.put("mail", properties.getProperty("message.mail"));
		contactsMap.put("mailvelho", properties.getProperty("message.mailvelho"));
		
		request.getSession().setAttribute("contactsMap", contactsMap);
		request.getSession().setAttribute("menuMap", menuMap);
	}

}
